package blagajna;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.Timer;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author root
 */
public class FrmPoslovniProstor extends javax.swing.JFrame {

    private Logger logger;
    
    private Thread sendThread;
    private Timer timer;
    private int count;
    private boolean pNazivStatus;
    
    private FormWindowAdapter windowAdapter;
    private FrmPostavke postavke;
    private FrmBlagajna blagajna;
    /**
     * Boja dugmica kada nema fokus
     */
    private Color boja1;
    /**
     * Boja dugmica kada ima fokus
     */
    private Color boja2;
    /**
     * Javni objekt za dohvat izgleda korisnickog sucelja.<BR> Font, velicina,
     * debljina, ... <BR>
     */
    public Izgled izgled;

    /**
     * Creates new form PoslovniProstorFrame
     */
    //public FrmPoslovniProstor(String keyFile, String keyPassword, String oib, String sURL, String poslovnica, String blagajna, String datumPocetkaPrimjene) {
    public FrmPoslovniProstor(FrmBlagajna blagajna) {

        this.blagajna = blagajna;
        postavke = blagajna.getFrmPostavke();        

        logger = blagajna.getLogger();

        this.windowAdapter = new FormWindowAdapter(blagajna, false);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.addWindowListener(windowAdapter);

        // preuzmi postavke iz frmPostavke prozora
//        this.keyFile = postavke.getKeyFile();
//        this.keyPassword = postavke.getKeyPassword();
//        this.oib = postavke.getOib();
//        this.sURL = postavke.getsURL();
//        this.poslovnica = postavke.getPoslovnica();
//        this.blagajna = postavke.getBlagajna();
//        this.datumPocetkaPrimjene = new SimpleDateFormat("dd.MM.yyyy",
//                Locale.ENGLISH).format(new Date());

        //this.timeout = postavke.getTimeout();

        initComponents();

        // postavi pocetne boje
        boja1 = cmdPrijavi.getBackground();
        boja2 = Color.ORANGE;

        ui();

        progresBar.setMaximum(postavke.getTimeout());
        progresBar.setMinimum(0);

        pNazivStatus = false;

        File ppXML = new File("pp.xml");

        if (ppXML.exists()) {

            XMLPoslovniProstor p1 = new XMLPoslovniProstor();
            p1.loadPoslovniProstor("pp.xml");

            if (p1.getOznakaZatvaranja().equalsIgnoreCase("Z")) {
                txtStatus.setText("Poslovni prostor je odjavljen. Prijavite novi poslovni prostor.");
            } else {

                if (p1.getGreska().length() > 0) {
                    txtStatus.setText(p1.getGreska());
                } else {
                    txtStatus.setText("Podaci za poslovni prostor ucitani.");
                }
            }

            txtUlica.setText(p1.getUlica());
            txtKucniBroj.setText(p1.getKucniBroj());
            txtDodatakKucniBroj.setText(p1.getDodatakKucnomBroju());
            txtBrojPoste.setText(p1.getBrojPoste());
            txtNaselje.setText(p1.getNaselje());
            txtOpcina.setText(p1.getOpcina());
            txtRadnoVrijeme.setText(p1.getRadnoVrijeme());

        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlIzbornik = new javax.swing.JPanel();
        cmdPrijavi = new javax.swing.JButton();
        cmdOdjavi = new javax.swing.JButton();
        cmdIzlaz = new javax.swing.JButton();
        pnlUnos = new javax.swing.JPanel();
        lblUlica = new javax.swing.JLabel();
        lblKucniBroj = new javax.swing.JLabel();
        lblDodatakKucnomBroju = new javax.swing.JLabel();
        lblBrojPoste = new javax.swing.JLabel();
        lblNaselje = new javax.swing.JLabel();
        lblOpcina = new javax.swing.JLabel();
        lblRadnoVrijeme = new javax.swing.JLabel();
        txtUlica = new javax.swing.JTextField();
        txtKucniBroj = new javax.swing.JTextField();
        txtDodatakKucniBroj = new javax.swing.JTextField();
        txtBrojPoste = new javax.swing.JTextField();
        txtNaselje = new javax.swing.JTextField();
        txtOpcina = new javax.swing.JTextField();
        txtRadnoVrijeme = new javax.swing.JTextField();
        pnlStatus = new javax.swing.JScrollPane();
        txtStatus = new javax.swing.JTextPane();
        progresBar = new javax.swing.JProgressBar();

        setTitle("Poslovni prostor");

        pnlIzbornik.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        cmdPrijavi.setText("Prijavi");
        cmdPrijavi.setToolTipText("Prijavi poslovni prostor");
        cmdPrijavi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdPrijaviActionPerformed(evt);
            }
        });
        cmdPrijavi.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                cmdPrijaviFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                cmdPrijaviFocusLost(evt);
            }
        });

        cmdOdjavi.setText("Odjavi");
        cmdOdjavi.setToolTipText("Odjavi poslovni prostor");
        cmdOdjavi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdOdjaviActionPerformed(evt);
            }
        });
        cmdOdjavi.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                cmdOdjaviFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                cmdOdjaviFocusLost(evt);
            }
        });

        cmdIzlaz.setText("Izlaz");
        cmdIzlaz.setToolTipText("Zatvori prozor i povratak na glavni prozor");
        cmdIzlaz.setMaximumSize(new java.awt.Dimension(78, 25));
        cmdIzlaz.setMinimumSize(new java.awt.Dimension(78, 25));
        cmdIzlaz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdIzlazActionPerformed(evt);
            }
        });
        cmdIzlaz.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                cmdIzlazFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                cmdIzlazFocusLost(evt);
            }
        });

        javax.swing.GroupLayout pnlIzbornikLayout = new javax.swing.GroupLayout(pnlIzbornik);
        pnlIzbornik.setLayout(pnlIzbornikLayout);
        pnlIzbornikLayout.setHorizontalGroup(
            pnlIzbornikLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(cmdPrijavi, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(cmdOdjavi, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(cmdIzlaz, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pnlIzbornikLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cmdIzlaz, cmdOdjavi, cmdPrijavi});

        pnlIzbornikLayout.setVerticalGroup(
            pnlIzbornikLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlIzbornikLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmdPrijavi, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmdOdjavi, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
                .addComponent(cmdIzlaz, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pnlIzbornikLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {cmdIzlaz, cmdOdjavi, cmdPrijavi});

        pnlUnos.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        lblUlica.setText("Ulica:");

        lblKucniBroj.setText("Kucni broj:");

        lblDodatakKucnomBroju.setText("Dodatak kucnom broju:");

        lblBrojPoste.setText("Broj poste:");

        lblNaselje.setText("Naselje:");

        lblOpcina.setText("Opcina:");

        lblRadnoVrijeme.setText("Radno vrijeme:");

        txtUlica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtUlicaActionPerformed(evt);
            }
        });
        txtUlica.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtUlicaFocusGained(evt);
            }
        });

        txtKucniBroj.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtKucniBrojFocusGained(evt);
            }
        });

        txtDodatakKucniBroj.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtDodatakKucniBrojFocusGained(evt);
            }
        });

        txtBrojPoste.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtBrojPosteFocusGained(evt);
            }
        });

        txtNaselje.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtNaseljeFocusGained(evt);
            }
        });

        txtOpcina.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtOpcinaActionPerformed(evt);
            }
        });
        txtOpcina.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtOpcinaFocusGained(evt);
            }
        });

        txtRadnoVrijeme.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtRadnoVrijemeFocusGained(evt);
            }
        });

        javax.swing.GroupLayout pnlUnosLayout = new javax.swing.GroupLayout(pnlUnos);
        pnlUnos.setLayout(pnlUnosLayout);
        pnlUnosLayout.setHorizontalGroup(
            pnlUnosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlUnosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlUnosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblUlica, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblKucniBroj, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblDodatakKucnomBroju, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblBrojPoste, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblNaselje, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblOpcina, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblRadnoVrijeme, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlUnosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtOpcina)
                    .addComponent(txtRadnoVrijeme, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtBrojPoste)
                    .addComponent(txtNaselje, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtUlica)
                    .addComponent(txtKucniBroj, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtDodatakKucniBroj, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        pnlUnosLayout.setVerticalGroup(
            pnlUnosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlUnosLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlUnosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUlica)
                    .addComponent(txtUlica, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlUnosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblKucniBroj)
                    .addComponent(txtKucniBroj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlUnosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDodatakKucnomBroju)
                    .addComponent(txtDodatakKucniBroj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlUnosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblBrojPoste)
                    .addComponent(txtBrojPoste, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlUnosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNaselje, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNaselje))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlUnosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtOpcina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblOpcina))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlUnosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtRadnoVrijeme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblRadnoVrijeme))
                .addGap(167, 167, 167))
        );

        txtStatus.setEditable(false);
        pnlStatus.setViewportView(txtStatus);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(progresBar, javax.swing.GroupLayout.DEFAULT_SIZE, 604, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlUnos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlIzbornik, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(12, 12, 12)
                    .addComponent(pnlStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 602, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlIzbornik, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlUnos, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progresBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(98, 98, 98))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(237, Short.MAX_VALUE)
                    .addComponent(pnlStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap()))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtUlicaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtUlicaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtUlicaActionPerformed

    private void txtOpcinaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtOpcinaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtOpcinaActionPerformed

    private void cmdPrijaviActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdPrijaviActionPerformed

        cmdPrijavi.setEnabled(false);
        cmdOdjavi.setEnabled(false);

        count = 0;
        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                count++;

                if (count > postavke.getTimeout()) {
                    sendThread.interrupt();
                    cmdPrijavi.setEnabled(true);
                    cmdOdjavi.setEnabled(true);
                    txtStatus.setText("Greska u slanju poslovnog prostora. Provjerite internet vezu.");
                    timer.stop();
                } else {
                    txtStatus.setText("Saljem poslovni prostor ... " + count + "/" + postavke.getTimeout());
                    progresBar.setValue(count);
                }
            }
        });

        sendThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(0);

                    XMLPoslovniProstor p1 = new XMLPoslovniProstor();

                    p1.setUlica(txtUlica.getText());
                    p1.setKucniBroj(txtKucniBroj.getText());
                    p1.setDodatakKucnomBroju(txtDodatakKucniBroj.getText());
                    p1.setBrojPoste(txtBrojPoste.getText());
                    p1.setNaselje(txtNaselje.getText());
                    p1.setOpcina(txtOpcina.getText());
                    p1.setRadnoVrijeme(txtRadnoVrijeme.getText());

                    p1.setDatumPocetkaPrimjene(new Date());

                    p1.setOib(postavke.getOib());
                    p1.setOznakaPoslovnogProstora(postavke.getPoslovnica());
                    p1.setURL(postavke.getsURL());

                    p1.buildPoslovniProstor();
                    p1.savePoslovniProstor("pp.xml");

                    p1.loadKeyStore(postavke.getKeyFile(), postavke.getKeyPassword());
                    p1.signPoslovniProstor();
                    p1.savePoslovniProstor("pp.signed.xml");

                    txtStatus.setText("Saljem poslovni prostor ...");
                    p1.sendPoslovniPorostor(postavke.getTimeout());
                    if (p1.getGreska().length() > 0) {
                        if (count < postavke.getTimeout()) {
                            txtStatus.setText(p1.getGreska());
                            logger.severe(p1.getGreska());
                            p1.savePoslovniProstor("pp.response.xml");
                            timer.stop();

                        }
                    } else {
                        txtStatus.setText("Prijava izvrsena.");
                        p1.savePoslovniProstor("pp.response.xml");
                        timer.stop();
                        progresBar.setValue(progresBar.getMaximum());

                    }

                    cmdPrijavi.setEnabled(true);
                    cmdOdjavi.setEnabled(true);

                } catch (InterruptedException ex) {
                    logger.log(Level.SEVERE, "cmdPrijavi()", ex);
                }
            }
        });
        
        // Provjeri HR znakove koji se ne smiju unijeti
        boolean greska = false;
        
        for(Component comp : pnlUnos.getComponents()) {
            if(comp instanceof JTextField) {
                JTextField unos = (JTextField) comp;
                // Trazi HR znakove u text poljima
                if(unos.getText().contains("Č") || unos.getText().contains("č") ) {
                    greska = true;
                }
                if(unos.getText().contains("Ć") || unos.getText().contains("ć")) {
                    greska = true;
                }
                if(unos.getText().contains("Đ") || unos.getText().contains("đ")) {
                    greska = true;
                }
                if(unos.getText().contains("Š") || unos.getText().contains("š")) {
                    greska = true;
                }
                if(unos.getText().contains("Ž") || unos.getText().contains("ž")) {
                    greska = true;
                }
                
            }
        }
        
        if (greska) {
            // Prikazi korisniku gresku
            JOptionPane.showMessageDialog(this, "HR znakovi nisu podrzani.", "Poslovni prostor", JOptionPane.ERROR_MESSAGE);
            // Logiraj gresku
            logger.info("HR znakovi nisu podrzani.");
            // Omoguci dugmice
            cmdPrijavi.setEnabled(true);
            cmdOdjavi.setEnabled(true);
        } else {
            timer.start();
            sendThread.start();
        }


    }//GEN-LAST:event_cmdPrijaviActionPerformed

    private void cmdOdjaviActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdOdjaviActionPerformed

        cmdPrijavi.setEnabled(false);
        cmdOdjavi.setEnabled(false);

        count = 0;
        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                count++;

                if (count <= postavke.getTimeout()) {
                    txtStatus.setText("Odjavljujem poslovni prostor ... " + count + "/" + postavke.getTimeout());
                    progresBar.setValue(count);
                } else {
                    sendThread.interrupt();
                    cmdPrijavi.setEnabled(true);
                    cmdOdjavi.setEnabled(true);
                    txtStatus.setText("Greska u odjavi poslovnog prostora. Provjerite internet vezu.");
                    timer.stop();
                }
            }
        });

        sendThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(0);

                    XMLPoslovniProstor p1 = new XMLPoslovniProstor();

                    p1.setUlica(txtUlica.getText());
                    p1.setKucniBroj(txtKucniBroj.getText());
                    p1.setDodatakKucnomBroju(txtDodatakKucniBroj.getText());
                    p1.setBrojPoste(txtBrojPoste.getText());
                    p1.setNaselje(txtNaselje.getText());
                    p1.setOpcina(txtOpcina.getText());
                    p1.setRadnoVrijeme(txtRadnoVrijeme.getText());

                    p1.setDatumPocetkaPrimjene(new Date());

                    p1.setOznakaZatvaranja();

                    p1.setOib(postavke.getOib());
                    p1.setOznakaPoslovnogProstora(postavke.getPoslovnica());
                    p1.setURL(postavke.getsURL());

                    p1.buildPoslovniProstor();
                    p1.savePoslovniProstor("pp.xml");

                    p1.loadKeyStore(postavke.getKeyFile(), postavke.getKeyPassword());
                    p1.signPoslovniProstor();
                    p1.savePoslovniProstor("pp.signed.xml");

                    txtStatus.setText("Odjavljujem poslovni prostor ...");
                    p1.sendPoslovniPorostor(postavke.getTimeout());
                    if (p1.getGreska().length() > 0) {
                        if (count < postavke.getTimeout()) {
                            txtStatus.setText(p1.getGreska());
                            logger.severe(p1.getGreska());
                            p1.savePoslovniProstor("pp.response.xml");
                            timer.stop();
                        }
                    } else {
                        progresBar.setValue(progresBar.getMaximum());
                        txtStatus.setText("Odjava izvrsena.");
                        p1.savePoslovniProstor("pp.response.xml");
                        timer.stop();
                    }
                    cmdPrijavi.setEnabled(true);
                    cmdOdjavi.setEnabled(true);

                } catch (InterruptedException ex) {
                    logger.log(Level.SEVERE, "cmdOdjavi()", ex);
                }
            }
        });
        
        // Provjeri HR znakove koji se ne smiju unijeti
        boolean greska = false;
        
        for(Component comp : pnlUnos.getComponents()) {
            if(comp instanceof JTextField) {
                JTextField unos = (JTextField) comp;
                // Trazi HR znakove u text poljima
                if(unos.getText().contains("Č") || unos.getText().contains("č") ) {
                    greska = true;
                }
                if(unos.getText().contains("Ć") || unos.getText().contains("ć")) {
                    greska = true;
                }
                if(unos.getText().contains("Đ") || unos.getText().contains("đ")) {
                    greska = true;
                }
                if(unos.getText().contains("Š") || unos.getText().contains("š")) {
                    greska = true;
                }
                if(unos.getText().contains("Ž") || unos.getText().contains("ž")) {
                    greska = true;
                }
                
            }
        }
        
        if (greska) {
            // Prikazi korisniku gresku
            JOptionPane.showMessageDialog(this, "HR znakovi nisu podrzani.", "Poslovni prostor", JOptionPane.ERROR_MESSAGE);
            // Logiraj gresku
            logger.info("HR znakovi nisu podrzani.");
            // Omoguci dugmice
            cmdPrijavi.setEnabled(true);
            cmdOdjavi.setEnabled(true);
        } else {
            timer.start();
            sendThread.start();
        }

    }//GEN-LAST:event_cmdOdjaviActionPerformed
    /**
     * Korisnicko sucelje postavlja.<BR>
     */
    public void ui() {
        // UI
        izgled = blagajna.izgled;
        
        // Panel sa dugmicima
        for (Component comp : pnlIzbornik.getComponents()) {
            if (comp instanceof JButton) {
                
                comp.setFont(new Font(izgled.getFontNaziv(), Font.PLAIN, izgled.getFontVelicina()));
                
                //comp.setMinimumSize(new Dimension(comp.getWidth(), izgled.getDebljinaDugmica()));
                //comp.setMaximumSize(new Dimension(comp.getWidth(), izgled.getDebljinaDugmica()));
                comp.setPreferredSize(new Dimension(comp.getWidth(), izgled.getDebljinaDugmica()));

            }
        }
        // Panel sa text poljima
        for (Component comp : pnlUnos.getComponents()) {
            if (comp instanceof JLabel || comp instanceof JTextField) {
                
                comp.setFont(new Font(izgled.getFontNaziv(), Font.PLAIN, izgled.getFontVelicina()));
                comp.setPreferredSize(new Dimension(comp.getWidth(), izgled.getDebljinaDugmica()));
                
            }
        }

        txtStatus.setFont(new Font(izgled.getFontNaziv(), Font.PLAIN, izgled.getFontVelicina()));

    }
    // funkcija za selektiranje txt polja

    private void selText(JTextField txtField) {

        txtField.setSelectionStart(0);
        txtField.setSelectionEnd(txtField.getText().length());

    }

    private void cmdIzlazActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdIzlazActionPerformed
        blagajna.toggleScreen(true);
        this.setVisible(false);

    }//GEN-LAST:event_cmdIzlazActionPerformed

    private void txtUlicaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtUlicaFocusGained

        if (pNazivStatus) {
            txtStatus.setText("Naziv ulice. Obavezan podatak.");
        } else {
            pNazivStatus = true;
        }
        selText(txtUlica);

    }//GEN-LAST:event_txtUlicaFocusGained

    private void txtKucniBrojFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtKucniBrojFocusGained
        txtStatus.setText("Kucni broj. Obavezan podatak. Ako ne postoji, svejedno staviti neki broj, npr. 1.");
        selText(txtKucniBroj);
    }//GEN-LAST:event_txtKucniBrojFocusGained

    private void txtDodatakKucniBrojFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDodatakKucniBrojFocusGained
        txtStatus.setText("Dodatak na kucni broj. Nije obavezan podatak. Npr. moze biti bb ako nema kucnog broja.");
        selText(txtDodatakKucniBroj);
    }//GEN-LAST:event_txtDodatakKucniBrojFocusGained

    private void txtBrojPosteFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtBrojPosteFocusGained
        txtStatus.setText("Broj poste. Obavezan podatak.");
        selText(txtBrojPoste);
    }//GEN-LAST:event_txtBrojPosteFocusGained

    private void txtNaseljeFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNaseljeFocusGained
        txtStatus.setText("Naziv naselja. Nije obavezan podatak.");
        selText(txtNaselje);
    }//GEN-LAST:event_txtNaseljeFocusGained

    private void txtOpcinaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtOpcinaFocusGained
        txtStatus.setText("Naziv opcine ili grada. Obavezan podatak.");
        selText(txtOpcina);
    }//GEN-LAST:event_txtOpcinaFocusGained

    private void txtRadnoVrijemeFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtRadnoVrijemeFocusGained
        txtStatus.setText("Radno vrijeme. Obavezan podatak. Npr. Pon 8h-16h,Uto 8h-16h,Sub 8h-13h.");
        selText(txtRadnoVrijeme);
    }//GEN-LAST:event_txtRadnoVrijemeFocusGained

    private void cmdPrijaviFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cmdPrijaviFocusGained
        cmdPrijavi.setBackground(boja2);
        txtStatus.setText("Zakonska je obveza prijaviti poslovni prostor prije izdavanja racuna.\nHR znakovi nisu podrzani.");
    }//GEN-LAST:event_cmdPrijaviFocusGained

    private void cmdPrijaviFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cmdPrijaviFocusLost
        cmdPrijavi.setBackground(boja1);
    }//GEN-LAST:event_cmdPrijaviFocusLost

    private void cmdOdjaviFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cmdOdjaviFocusGained
        cmdOdjavi.setBackground(boja2);
        txtStatus.setText("Mozete odjaviti vec prijavljen poslovni prostor.\nHR znakovi nisu podrzani.");
    }//GEN-LAST:event_cmdOdjaviFocusGained

    private void cmdOdjaviFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cmdOdjaviFocusLost
        cmdOdjavi.setBackground(boja1);
    }//GEN-LAST:event_cmdOdjaviFocusLost

    private void cmdIzlazFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cmdIzlazFocusGained
        cmdIzlaz.setBackground(boja2);
    }//GEN-LAST:event_cmdIzlazFocusGained

    private void cmdIzlazFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cmdIzlazFocusLost
        cmdIzlaz.setBackground(boja1);
    }//GEN-LAST:event_cmdIzlazFocusLost
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdIzlaz;
    private javax.swing.JButton cmdOdjavi;
    private javax.swing.JButton cmdPrijavi;
    private javax.swing.JLabel lblBrojPoste;
    private javax.swing.JLabel lblDodatakKucnomBroju;
    private javax.swing.JLabel lblKucniBroj;
    private javax.swing.JLabel lblNaselje;
    private javax.swing.JLabel lblOpcina;
    private javax.swing.JLabel lblRadnoVrijeme;
    private javax.swing.JLabel lblUlica;
    private javax.swing.JPanel pnlIzbornik;
    private javax.swing.JScrollPane pnlStatus;
    private javax.swing.JPanel pnlUnos;
    private javax.swing.JProgressBar progresBar;
    private javax.swing.JTextField txtBrojPoste;
    private javax.swing.JTextField txtDodatakKucniBroj;
    private javax.swing.JTextField txtKucniBroj;
    private javax.swing.JTextField txtNaselje;
    private javax.swing.JTextField txtOpcina;
    private javax.swing.JTextField txtRadnoVrijeme;
    private javax.swing.JTextPane txtStatus;
    private javax.swing.JTextField txtUlica;
    // End of variables declaration//GEN-END:variables
}
